<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>全员销售额</span>
    </div>
    <v-searchForm
      :form-data="filter_info"
      :form-title="formTitle"
      @search="search"
      @reset="reset"/>

    <v-tableBtns
      :table-title="exportTitle"
      :table-data="exportData"
    />

    <v-table
      :table-title="tableTitle"
      :table-data="tableData"
      :default-operate="false"
      :select="false"
      btn-class="btn"
      @sortChange="sortChange"
    />
    <!--<v-pagination-->
    <!--:pageInfo="pageInfo"-->
    <!--:getTableList="getTableList"-->
    <!--/>-->
  </el-card>
</template>

<script>
  import {parseTime} from '../../../utils'

  export default {
    name: 'Index',
    data() {
      const now = new Date();
      now.setTime(now.getTime() - 3600 * 1000 * 24 * (now.getDate() - 1));
      const startTime = parseTime(now, '{y}-{m}-{d}');
      const endTime = parseTime(new Date(now.getFullYear(),now.getMonth()+1,0),'{y}-{m}-{d}')
      return {
        // f分页
        pageInfo: {
          pageNumber: 1,
          pageSize: 30,
          totalPage: 0,
          totalRow: 0
        },

        sidx: 'bm_name',
        sortKey: null,
        exportTitle: [],
        exportData: [],
        filter_info: {
          created_at: [startTime, endTime]
        },
        originFilter: {
          created_at: [startTime, endTime]
        },
        swList: [],
        tableData: [],
        departList:[]

      }
    },
    computed: {
      tableTitle() {
        return ([
          {
            cn: '编号',
            en: 'swid'
          },
          {
            cn: '人员名称',
            en: 'swname',
            formatter: (row, column) => {
              if (row.swname) {
                return row.swname
              }
              let swname = ''
              this.swList.forEach(item => {
                if (item.value === row.swid) {
                  swname = item.label
                }
              })
              return swname
            }
          },
          {
            cn: '部门',
            en: 'bm_name',
            sortable: true
          },
          {
            cn: '销售额',
            en: 'saled',
            sortable: true
          },
          {
            cn: '回款额',
            en: 'returned',
            sortable: true
          },
          {
            cn: '回款比例',
            en: 'ratio',
            sortable: true
          },
          {
            cn: '订单数量',
            en: 'num',
            sortable: true
          }
        ])
      },
      filter_infos() {
        const data = {}
        const origin = this.filter_info
        for (const key in origin) {
          if (!this.filter_info[key] && this.filter_info[key] !== 0) {
            this.$delete(this.filter_info, key)
            continue
          }
          if (key === 'created_at') {
            data.start_time = origin[key][0]
            data.End_time = origin[key][1]
          } else {
            data[key] = origin[key]
          }
        }
        if (!origin.created_at) {
          delete data.start_time
          delete data.End_time
        }
        return data
      },
      formTitle() {
        return ([
          {
            cn: '人员名称：',
            en: 'swid',
            type: 'select',
            list: this.swList
          },
          {
            cn: '仅查看部门：',
            en: 'dd_id',
            type: 'select',
            list: this.departList
          },
          {
            cn: '时间：',
            en: 'created_at',
            type: 'time'
          }
        ])
      },
    },
    activated() {
      this.getTableList()
      this.getSwList()
      this.getDepartList()
    },
    methods: {
      getTableList() {
        const datas = {
          ...this.filter_infos,
          sidx: this.sidx,
          sortKey: this.sortKey,
          page: this.pageInfo.pageNumber, rows: this.pageInfo.pageSize
        }
        this.$get('action/WechatAdvs/fansAndmapsForm2', datas)
          .then(res => {
            const list1 = res.data.ad_price
            this.tableData = list1.map(item => {
              const obj = item
              obj.ratio = item.ratio || 0
              return obj
            })
            // this.pageInfo.totalPage = res.data.totalPage;
            // this.pageInfo.totalRow = res.data.totalRow;
          })
      },
      // 获取商务列表
      getSwList() {
        this.$get('action/WxUserCtrl/staffinfo')
          .then(res => {
            const list = []
            if (res.data) {
              res.data.staffinfotree.forEach(item => {
                list.push({
                  label: item.re_name,
                  value: item.id
                })
              })
            }
            this.swList = list
          })
      },
      getDepartList(){

        this.$get('action/StaffinfoCtrl/staff_group')
          .then(res => {
            let list
            if (res.data.list && res.data.list.length) {
              list = res.data.list.map((item, index) => {
                return {
                  label:item.bm_name,
                  value: item.id
                }
              })
            }
            this.departList = list || []
          })
      },

      // 搜索
      search() {
        this.pageInfo.pageNumber=1
        this.getTableList()
      },

      reset() {
        this.filter_info = {}
        this.getTableList()
      },

      sortChange({column, prop, order}) {
        this.sidx = prop || 'bm_name'
        this.sortKey = order === 'ascending' ? 'asc' : null
        this.getTableList()
      }
    }
  }
</script>
